#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_ddl.py 
@version:
@time: 2022/08/10 
"""
from TapClientCore.T07_CaseTemplate.v2.CT03_task.ct_task import *
from TapClientCore.T07_CaseTemplate.v2.CT01_datasource.ct_datasource import *


class CTTaskDDL(CTTask, CTDataSource):

    def TASK_BASE(self, parallel_data):
        data = None
        record_id = None
        task_name = None
        source_info = None
        source_table = None
        target_info = None
        target_table = None
        task_record = None
        source_delete = None
        sql_ddl = parallel_data.get('sql_ddl')
        # 随机等待
        # self.random_wait()
        try:
            # 并行案例初始化
            with allure.step('获取可用表'):
                data = self.lock_and_get_table(parallel_data)
            record_id = data.get('id')

            operate = data.pop('operate')
            # source
            operate_source = operate.get('source')
            source_info = operate_source.get('source_info')
            source_table = operate_source.get('table')
            # target
            operate_target = operate.get('target')
            target_info = operate_target.get('target_info')
            target_table = operate_target.get('table')

            # DML DATA
            source_insert_column = operate_source.get('insert')
            source_update_column = operate_source.get('update')
            source_update_column2 = operate_source.get('update2')
            source_where_column = operate_source.get('where')

            data['task_config']['config']['name'] += source_table
            with allure.step('任务名获取'):
                task_name = data.get('task_config').get('config').get('name')

            # connector
            with allure.step('源数据库初始化'):
                _source = self.init_connector(source_info)
                _source.tapclient_connector.CURRENT_TABLE = source_table
            with allure.step('目标数据库初始化'):
                _target = self.init_connector(target_info)
                _target.tapclient_connector.CURRENT_TABLE = target_table

            # DML SQL
            with allure.step('DML generate'):
                source_insert = _source.tapclient_connector.generate_insert(
                    source_insert_column
                )
                source_update1 = _source.tapclient_connector.generate_update(
                    source_update_column,
                    source_where_column
                )
                source_update2 = _source.tapclient_connector.generate_update(
                    source_update_column2,
                    source_where_column
                )
                source_delete = _source.tapclient_connector.generate_delete(
                    source_where_column
                )
            with allure.step('DDL generate'):
                ddl_add = _source.tapclient_connector.generate_alter_add(source_table, *sql_ddl.get('add'))
                ddl_modify = _source.tapclient_connector.generate_alter_modify(source_table, *sql_ddl.get('modify'))
                ddl_change = _source.tapclient_connector.generate_alter_change(source_table, *sql_ddl.get('change'))
                ddl_drop = _source.tapclient_connector.generate_alter_drop(source_table, *sql_ddl.get('drop'))
                ddl_init_drop = _source.tapclient_connector.generate_alter_drop(source_table, sql_ddl.get('modify')[0])

            # stats check
            _stats = operate.get('stats_check').get('initial+cdc')
            _stats_initial = _stats.get('initial')
            _stats_insert = _stats.get('insert')
            _stats_update = _stats.get('update')
            _stats_delete = _stats.get('delete')

            # 删除表后重新加载模型
            self.drop_target_table(_target)
            self.datasource_test_by_db_info(target_info)

            # 任务运行
            with allure.step('创建任务'):
                task_record = self.create_data_copy_development(data)
            with allure.step('等待任务运行'):
                self.scheduled_task_wait_it_running(task_name)
            # 全量阶段校验
            logger.info('initial check')
            self.func_wait(
                self.get_task_stats_insert,
                self.get_task_status,
                task_name,
                _stats_initial
            )
            # check
            self.tap_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            # ddl add
            logger.info('ddl add')
            _source.tapclient_connector.exec(ddl_add)
            _source.tapclient_connector.exec(source_insert)
            self.func_wait(
                self.get_task_stats_insert,
                self.get_task_status,
                task_name,
                _stats_insert
            )
            self.tap_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            self.column_check(
                source_info,
                source_table,
                target_info,
                target_table,
                lambda x: x,
            )
            # ddl modify
            logger.info('ddl modify')
            _source.tapclient_connector.exec(ddl_modify)
            _source.tapclient_connector.exec(source_update1)
            self.func_wait(
                self.get_task_stats_update,
                self.get_task_status,
                task_name,
                _stats_update
            )
            self.tap_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            # ddl change
            logger.info('ddl change')
            _source.tapclient_connector.exec(ddl_change)
            _source.tapclient_connector.exec(source_update2)
            self.func_wait(
                self.get_task_stats_update,
                self.get_task_status,
                task_name,
                # 这里是update 的数值，没有单独配置，所以沿用了insert的
                _stats_insert
            )
            self.tap_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
            self.column_check(
                source_info,
                source_table,
                target_info,
                target_table,
                lambda x: x,
            )
            # ddl drop
            logger.info('ddl drop')
            _source.tapclient_connector.exec(ddl_drop)
            _source.tapclient_connector.exec(source_delete)
            self.func_wait(
                self.get_task_stats_delete,
                self.get_task_status,
                task_name,
                _stats_delete
            )
            self.tap_check(
                source_info,
                source_table,
                target_info,
                target_table
            )
        except Exception as e:
            self.exception_attach(task_name, e)
        finally:
            self.release_table(record_id)
            self.generate_task_url(task_name, data.get('task_type'))
            _source.tapclient_connector.exec(source_delete)
            try:
                _source.tapclient_connector.exec(ddl_drop)
            except Exception:
                pass
            try:
                _source.tapclient_connector.exec(ddl_init_drop)
            except Exception:
                pass
            self.datasource_test_by_db_info(source_info)

            if self.task_is_running(task_name):
                self.stop_task_wait_it_paused(task_name)
            try:
                _source.tapclient_connector.close()
                _target.tapclient_connector.close()
            except Exception as e:
                pass
